package com.cyk.comicforumjava.demos.config;

import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class QiniuKey {
    public static final String ACCESS_KEY = "V7L8WNLppqa_3zY6K6mo2vcANa65Oh0jsexcCFOU";
    public static final String SECRET_KEY = "P4QBsDPSTTGQ6Netb_mF80fGxLJubfM_boXWH6OF";
    public static final String BUCKET_NAME = "comic-forum";
   public static String upload() throws AuthException, JSONException {
       Config.ACCESS_KEY = ACCESS_KEY;
       Config.SECRET_KEY = SECRET_KEY;
       Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
       // 请确保该bucket已经存在
       String bucketName = BUCKET_NAME;
       PutPolicy putPolicy = new PutPolicy(bucketName);
       String uptoken = putPolicy.token(mac);

       return uptoken;
   }
   public static String finalurl(String key) throws UnsupportedEncodingException {
       String fileName = key;
// domainOfBucket 中的域名为用户 bucket 绑定的下载域名，下面域名仅为示例，不可使用
       String domainOfBucket = "http://svftekle7.hn-bkt.clouddn.com";
       String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
       String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
       System.out.println(finalUrl);
       return finalUrl;
   }
}
